{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2020-03-09 14:48:43,645: INFO: running neg files.\n",
      "2020-03-09 14:48:44,395: INFO: Saved 1000 files.\n",
      "2020-03-09 14:48:44,410: INFO: running pos files.\n",
      "2020-03-09 14:48:45,160: INFO: Saved 1000 files.\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8  -*-\n",
    "import logging\n",
    "import os,os.path\n",
    "import codecs,sys\n",
    "\n",
    "#读取文件内容\n",
    "def getContent(fullname):\n",
    "    f = open(fullname,'rb+')\n",
    "    content = f.readlines()\n",
    "    f.close()\n",
    "    return content\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    program = os.path.basename(sys.argv[0])#得到文件名\n",
    "    logger = logging.getLogger(program)\n",
    "    logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')\n",
    "    logging.root.setLevel(level=logging.INFO)\n",
    "    \n",
    "    #输入文件目录\n",
    "    inp = 'data\\ChnSentiCorp_htl_ba_2000' \n",
    "    folders = ['neg','pos']\n",
    "\n",
    "    for foldername in folders:\n",
    "        logger.info(\"running \"+ foldername +\" files.\")\n",
    "        \n",
    "        outp = '2000_' + foldername +'.txt' #输出文件\n",
    "        output = open(outp, 'wb')\n",
    "        i = 0\n",
    "        \n",
    "        rootdir = inp + '\\\\' + foldername\n",
    "        #三个参数：分别返回1.父目录 2.所有文件夹名字（不含路径） 3.所有文件名字\n",
    "        for parent,dirnames,filenames in os.walk(rootdir):\n",
    "            for filename in filenames:\n",
    "                content = getContent(rootdir + '\\\\' + filename)\n",
    "                output.writelines(content)\n",
    "                i = i+1\n",
    "                \n",
    "        output.close()\n",
    "        logger.info(\"Saved \"+str(i)+\" files.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow",
   "language": "python",
   "name": "tensorflow"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
